#ifndef WinError_h__
#define WinError_h__

#include <string>
#include <sstream>
#include <Windows.h>

#include <boost/shared_ptr.hpp>

namespace utils
{
    inline std::string FormatError(DWORD status, DWORD dwLanguageId = MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US))
    {
        LPVOID lpMsgBuf;
        if (DWORD dwSize = FormatMessageA(
            FORMAT_MESSAGE_ALLOCATE_BUFFER |
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            status,
            dwLanguageId, // Default language
            (LPSTR) &lpMsgBuf,
            0,
            NULL ))
        {
            boost::shared_ptr<void> guard(lpMsgBuf, LocalFree);

            if(dwSize < 2) 
                return "";

            return std::string((char*)lpMsgBuf, (char*)lpMsgBuf + dwSize - 2);
        }
        else
        {
            std::stringstream strBuilder;
            strBuilder<<"Can't format message from error code = "<<status;
            return strBuilder.str();
        }
    }

//////////////////////////////////////////////////////////////////////////

    inline std::string FormatLastError()
    {
        return FormatError(GetLastError());
    }

//////////////////////////////////////////////////////////////////////////
} // namespace utils


#endif // WinError_h__